package com.ms.education.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ms.education.course.entity.CourseCollect;
import com.ms.education.course.mapper.CourseCollectMapper;
import com.ms.education.course.service.CourseCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程收藏 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-02-16
 */
@Service
public class CourseCollectServiceImp extends ServiceImpl<CourseCollectMapper, CourseCollect> implements CourseCollectService {
    @Override
    public boolean isUserHasCourse(Integer userId, Integer courseId) {
        QueryWrapper<CourseCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("course_id", courseId);
        CourseCollect one = getOne(wrapper);
        return one != null;
    }

    @Override
    public void cancelCollect(Integer courseId, int userId) {
        UpdateWrapper<CourseCollect> wrapper = new UpdateWrapper<CourseCollect>().eq("course_id", courseId).eq("user_id", userId);
        remove(wrapper);
    }

    @Override
    public void collectCourse(Integer courseId, int userId) {
        CourseCollect courseCollect = new CourseCollect();
        courseCollect.setUserId(userId);
        courseCollect.setCourseId(courseId);
        save(courseCollect);
    }
}
